package com.furnitur.modules.portal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furnitur.modules.portal.dto.furniture.FurnitureQueryDTO;
import com.furnitur.modules.portal.mapper.PortalFurnitureMapper;
import com.furnitur.modules.portal.service.PortalFurnitureService;
import com.furnitur.modules.portal.vo.furniture.FurnitureVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class PortalFurnitureServiceImpl implements PortalFurnitureService {

    @Autowired
    private PortalFurnitureMapper portalFurnitureMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String FURNITURE_DETAIL_KEY = "furniture:detail:";
    private static final long CACHE_EXPIRE_TIME = 3600L; // 1小时

    @Override
    public Page<FurnitureVO> listFurniture(FurnitureQueryDTO queryDTO) {
        Page<FurnitureVO> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return portalFurnitureMapper.selectFurniturePage(page, queryDTO);
    }

    @Override
    public FurnitureVO getFurnitureDetail(Long id) {
        if (id == null) {
            throw new RuntimeException("商品ID不能为空");
        }

        // 先从缓存中获取
        String cacheKey = FURNITURE_DETAIL_KEY + id;
        FurnitureVO furniture = (FurnitureVO) redisTemplate.opsForValue().get(cacheKey);

        if (furniture != null) {
            return furniture;
        }

        // 缓存中没有，从数据库查询
        furniture = portalFurnitureMapper.selectFurnitureDetail(id);

        if (furniture == null) {
            throw new RuntimeException("商品不存在或已下架");
        }

        // 放入缓存
        redisTemplate.opsForValue().set(cacheKey, furniture, CACHE_EXPIRE_TIME, TimeUnit.SECONDS);

        return furniture;
    }

    @Override
    public Page<FurnitureVO> listHotFurniture(Integer pageSize) {
        FurnitureQueryDTO queryDTO = new FurnitureQueryDTO();
        queryDTO.setPageNum(1);
        queryDTO.setPageSize(pageSize);
        queryDTO.setSortField("sales_count");
        queryDTO.setSortOrder("desc");
        return listFurniture(queryDTO);
    }

    @Override
    public Page<FurnitureVO> listNewFurniture(Integer pageSize) {
        FurnitureQueryDTO queryDTO = new FurnitureQueryDTO();
        queryDTO.setPageNum(1);
        queryDTO.setPageSize(pageSize);
        queryDTO.setSortField("created_at");
        queryDTO.setSortOrder("desc");
        return listFurniture(queryDTO);
    }

    @Override
    @Cacheable(value = "furniture", key = "'new_' + #page.current + '_' + #page.size + '_' + #sortType")
    public IPage<FurnitureVO> getNewFurniture(Page<FurnitureVO> page, String sortType) {
        log.info("Loading new furniture with page: {}, sortType: {}", page, sortType);
        return portalFurnitureMapper.selectNewFurniture(page, sortType);
    }

    @Override
    @Cacheable(value = "furniture", key = "'hot_' + #pageSize")
    public Page<FurnitureVO> getHotFurniture(Integer pageSize) {
        log.info("Loading hot furniture with pageSize: {}", pageSize);
        Page<FurnitureVO> page = new Page<>(1, pageSize);
        return portalFurnitureMapper.selectHotFurniture(page);
    }
}